package storageManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class TableOptions {//This class has the methods that interacts with the user acquire the information about tables to do table operations
	SystemCatologReport syscat;
	public TableOptions() throws IOException{
//		syscat = new SystemCatologReport("syscatreport.txt");
		syscat = new SystemCatologReport("syscatreport.cat");
	}
	public void addTable() throws IOException{//Adds a table to system catalog file
		String tableName = "";
		int numberOfFields = 0;
		boolean ok = false;
		ArrayList<String> fieldNames = new ArrayList<String>();
		ArrayList<Integer> fieldSizes = new ArrayList<Integer>();
		Scanner scan = new Scanner(System.in);
		while(!ok){
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!Tools.checkTheName(27, tableName, "table name")){
				ok = false;
			}else if(syscat.isInFile(tableName)>=0){
				System.out.println("There is a table named "+tableName+"!");
				ok = false;
			}else{
				ok = true;
			}
		}
		ok=false;
		while(!ok){
			System.out.println("Please enter the number of fields:(Integer) ");
			numberOfFields = scan.nextInt();
			if(numberOfFields>13 || numberOfFields<2){
				System.out.println("Number of fields must be between 2 and 13!");
			}else{
				ok=true;
			}
		}
		boolean ok1=false;
		while(!ok1){
			fieldSizes.clear();
			fieldNames.clear();
			int totalSizes=0;
			System.out.println("Enter the names and sizes of the fields(First name you enter is going to be the name of the key field): ");
			for(int i=0; i<numberOfFields; i++){
				ok=false;
				while(!ok){
					System.out.println("Enter the "+(i+1)+". field's name: ");
					String name = scan.next();
					if(!Tools.checkTheName(16, name, "field name")){
						
					}else{
						ok =true;
						fieldNames.add(name);
					}	
				}
				ok = false;
				while(!ok){
					System.out.println("Enter the "+(i+1)+". field's size:(Integer) ");
					int size = scan.nextInt();
					if(size<0){
						System.out.println("A size of a field cannot be less than zero!");
					}
					else{
						ok = true;
						fieldSizes.add(size);
						totalSizes += size;
					}
				}
			}
			if(totalSizes<127){
				ok1=true;
			}
		}	
		TableRecord record = new TableRecord(tableName,numberOfFields,fieldNames,fieldSizes);
		syscat.addTable(record);
	}
	public void deleteTable() throws IOException{//Logically deletes a table
		String tableName = "";
		boolean ok = false;
		int page = -1;
		Scanner scan = new Scanner(System.in);
		while(!ok){
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!Tools.checkTheName(27, tableName, "table name")){
				ok = false;
			}else if((page = syscat.isInFile(tableName))<0){
				System.out.println("There is not a table named "+tableName+"!");
			}else{
				ok = true;
			}
		}
		syscat.deleteATable(page/2, tableName);
	}
	public void listAllTables() throws IOException{
		ArrayList<String> list = syscat.listOfAllTables();
		Collections.sort(list);
		for(String s : list){
			System.out.println(s);
		}
	}
	public TableRecord selectATableRecord() throws IOException{//Returns a table record object of which table name is decided by the user
		String tableName = "";
		boolean ok=false;
		Scanner scan = new Scanner(System.in);
		while(!ok){
			System.out.println("Please enter the name of the table: ");
			tableName = scan.nextLine();
			if(!Tools.checkTheName(27,tableName,"table name")){
				ok = false;
			}else if(syscat.isInFile(tableName)<0){
				ok = false;
			}else{
				ok = true;
			}
		}
		return syscat.findTheRecord(tableName);
	}
}
